﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;

namespace WineStore.Controllers
{
    public class SettingController : Controller
    {
        //
        // GET: /Setting/ShowSetting/
        public ViewResult ShowSetting()
        {
            using (var db = new WineDbContext())
            {
                var settings = db.Settings.ToList();
                return View(settings);
            }
        }

        // GET: /Setting/EditSetting/
        [HttpGet]
        public ActionResult EditSetting(int id = 0)
        {
            Setting setting = null;
            using (var db = new WineDbContext())
            {
                setting = db.Settings.Find(id);
                if (setting == null)
                {
                    return HttpNotFound();
                }
            }
            return View(setting);
        }
        // POST: /Setting/EditSetting/
        [HttpPost]
        public ActionResult EditSetting(Setting setting)
        {
            try
            {
                using (var db = new WineDbContext())
                {
                    db.Entry(setting).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowSetting", "Setting");
                }

            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(setting);
        }
    }
}
